package com.cfpl.security;

import cn.hutool.json.JSONUtil;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.result.BaseResult;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: LYP
 * @Date: 2023/10/12 - 11:13
 * @Description: com.cfpl.admin_service.security
 * @version: 1.0
 * 处理器响应结果
 */
public class HandlerResponse {

    /**
     * 登录成功
     * @param response response
     * @throws IOException yes
     */
    public static void responseSuccess( HttpServletResponse response,BaseResult<Object> result) throws IOException {
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSONUtil.toJsonStr(result));
    }

    /**
     * 登录失败
     * @param response response
     * @throws IOException yes
     */
    public static void responseFailed( HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=utf-8");
        BaseResult<Object> loginResult = new BaseResult<>(CodeEnum.USERNAME_OR_PASSWORD_ERROR);
        response.getWriter().write(JSONUtil.toJsonStr(loginResult));

    }


    /**
     * 权限不足
     * @param response response
     * @throws IOException yes
     */
    public static void responseInsufficientPermission( HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=utf-8");
        BaseResult<Object> loginResult = new BaseResult<>(CodeEnum.INSUFFICIENT_PERMISSION_ERROR);
        response.getWriter().write(JSONUtil.toJsonStr(loginResult));
    }

    /**
     * 注销成功
     * @param response response
     * @throws IOException yes
     */
    public static void responseLogOut( HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=utf-8");
        BaseResult<Object> loginResult = new BaseResult<>(200, "注销成功", null);
        response.getWriter().write(JSONUtil.toJsonStr(loginResult));
    }

    /**
     * 用户未登录
     * @param response response
     * @throws IOException yes
     */
    public static void responseUserNotLogin( HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=utf-8");
        BaseResult<Object> loginResult = new BaseResult<>(CodeEnum.USER_NOT_LOGIN);
        response.getWriter().write(JSONUtil.toJsonStr(loginResult));
    }

    /**
     * 用户异地登录
     * @param response response
     * @throws IOException yes
     */
    public static void responseUserAccountLoginAtOthers( HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=utf-8");
        BaseResult<Object> result = new BaseResult<>(501,"账号下线",null);
        response.getWriter().write(JSONUtil.toJsonStr(result));
    }
}
